<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_jadwal extends CI_Model{

	function list_jadwal($option,$id = 0,$page=1,$rows=10, $kondisi = "0",$jadwal_namaproyek =''){
		if ($option == "single"){
			$sql = "
			SELECT * FROM t_jadwal 
			where jadwal_id = '".mysql_real_escape_string($id)."'
			ORDER BY jadwal_id DESC ";
			$query = $this->db->query($sql);
			$result = $query->result();
			return json_encode($result);
		}else{
			$sql = "
			SELECT jadwal_id, jadwal_namaproyek ,jadwal_tglaan, IF (kondisi_nama ='PQ', concat('(PQ) ',jadwal_tglpq) ,jadwal_tglpemasukan) as  jadwal_tglpemasukan, jadwal_tglklarifikasi ,jadwal_tglkontrak,
			kondisi_nama
			FROM t_jadwal LEFT JOIN m_kondisi
			ON m_kondisi_id = kondisi_id WHERE";
			
			IF ($jadwal_namaproyek != ""){
				$sql .= " jadwal_namaproyek like '%".$jadwal_namaproyek."%' AND ";
				$kondisi = "0";
			}
			if ($kondisi <> "0")
				$sql .= " m_kondisi_id = '".mysql_real_escape_string($kondisi)."'";
			else
				$sql .= " true ";
			$sql .= "
			ORDER BY jadwal_id DESC ";
			
			$query = $this->db->query($sql);
			$records = $query->num_rows();
			
			$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows).",".$rows;
			$query = $this->db->query($sql);
			$result = $query->result();
			// '.ceil($records - $rows).'
			return '{ 
			  "total": "'.ceil($records / $rows).'", 
			  "page": "'.$page.'", 
			  "records": "'.$records .'",
			  "rows" : '.json_encode($result).'
			}';
		}
	}
	
	function getKondisi($task = null){
		$sql = "SELECT kondisi_id, kondisi_nama FROM m_kondisi";
		$query = $this->db->query($sql);
		$result = $query->result();
		$data = null;
		switch($task){
			case "combo" : 
				$data = array( "0" => "Semua");
				foreach($result as $row){
					$data[$row->kondisi_id] = $row->kondisi_nama;
				}
			break;
			default :
				$data = $result;
		}
		return $data;
	}
}